package net.schmurgon.gwt.scaffold.generated;

import com.google.gwt.app.place.RecordDetailsView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import net.schmurgon.gwt.request.SeriesRecord;
import net.schmurgon.gwt.request.CourseRecord;

/**
 * Details view for Series records.
 */
public class SeriesDetailsView extends Composite implements RecordDetailsView<SeriesRecord> {
	interface Binder extends UiBinder<HTMLPanel, SeriesDetailsView> {}

	private static final Binder BINDER = GWT.create(Binder.class);

	SeriesRecord record;
	
	@UiField SpanElement longCourse;
	@UiField SpanElement seriesName;
	@UiField SpanElement shortCourse;	
	@UiField SpanElement displayRenderer;
	@UiField HasClickHandlers edit;
	@UiField HasClickHandlers delete;

	private Delegate delegate;

	public SeriesDetailsView() {
		initWidget(BINDER.createAndBindUi(this));
	}

  	public Widget asWidget() {
		return this;
	}
  
	public boolean confirm(String msg) {
		return Window.confirm(msg);
	}

	public SeriesRecord getValue() {
		return record;
	}

	@UiHandler("delete")
	public void onDeleteClicked(ClickEvent e) {
		delegate.deleteClicked();
	}

	@UiHandler("edit")
	public void onEditClicked(ClickEvent e) {
		delegate.editClicked();
	}

	public void setDelegate(Delegate delegate) {
		this.delegate = delegate;
	}

	public void setValue(SeriesRecord record) {
		this.record = record;
	    longCourse.setInnerText(net.schmurgon.gwt.scaffold.generated.CourseRecordRenderer.instance().render(record.getLongCourse()));
	    seriesName.setInnerText(String.valueOf(record.getSeriesName()));
	    shortCourse.setInnerText(net.schmurgon.gwt.scaffold.generated.CourseRecordRenderer.instance().render(record.getShortCourse()));

		displayRenderer.setInnerText(net.schmurgon.gwt.scaffold.generated.SeriesRecordRenderer.instance().render(record));
	}
}
